home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char c,s[256],z[256],e[256];
- int i,n,o;
-
- if(argc==1) { /* without parameter return instructions */
- printf("strhead returns the first part of a string, up to a terminator\n");
- printf("specified as second parameter\n");
- printf("strhead abcde.fgh .fg\n will produce \n abcde \n");
- printf("you may specify an offset as optional 3rd parameter\n");
- printf("\n(C) Rainer Kowallik\n");
- }
-
- o=0;
- if(argc==4) o=atoi(argv[3]);
-
- strcpy(s,argv[1]); /* source string */
- strcpy(z,argv[2]); /* terminator string */
- i=instr(z,s);
- strcpy(e,s);
- if(i>0) midstr(e,s,0,i-1+o);
- printf("%s\n",e);
- exit(0);
- }
-
- /* -------------------------------------------
- return position of a substring in a string
- ------------------------------------------- */
- instr(substr,str)
- char str[],substr[];
- { short i,p,flg,l1,l2;
-
- l1=strlen(str); l2=strlen(substr);
- for(p=0; p < l1; p++) {
- flg=0;
- for(i=0; i < l2; i++) {
- if(str[p+i] != substr[i]) {
- flg = -1; break;
- }
- }
- if(flg == 0) return(p);
- }
- return(-1);
- }
-
-
- /* -------------------------------------------------
- return a substring from within a mainstring
- ------------------------------------------------- */
- midstr(substr,str,ss,es)
- char substr[],str[];
- short ss,es;
- { short i,j;
-
- i=0;
- for(j=ss; j <= es; j++) substr[i++]=str[j];
- substr[i]=0;
- }
-
-